package com.sakiko.listener;

import com.rabbitmq.client.Channel;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

/**
 * projectName: com.sakiko.listener
 *
 * @author: Yang
 * description: 消费者
 * time: 2025/8/16 16:38
 * todo: 发布订阅模式与工作队列模式的区别
 *  工作队列模式本质上是绑定默认交换机，监听同一个队列的消费端程序彼此之间是竞争关系
 *  发布订阅模式绑定指定交换机，绑定同一个交换机的多个队列在发布订阅模式下，消息是广播的，每个队列都能接收到消息
 */
@Component
public class MQListener {

    public static final String SIMPLE_QUEUE_NAME = "sakiko.queue.simple";

    // 【简单模式】 监听队列，获取信息进行处理
    @RabbitListener(queues = {SIMPLE_QUEUE_NAME})
    public void msgHandler(String message) {
        System.out.println("message = " + message);
    }

    // 【工作模式】
    public static final String WORK_QUEUE_NAME = "sakiko.queue.work";

    @Value("${server.port}")
    private String serverPort;

    @RabbitListener(queues = WORK_QUEUE_NAME)
    public void processMessage(String messageContent, Message message, Channel channel) {
        System.out.println("Server Port:" + serverPort + " Message Content:" + messageContent);
    }

    // 【广播模式】
    @RabbitListener(queues = {"sakiko.queue.fanout01"})
    public void processMessage01(String messageContent, Message message, Channel channel) {
        System.out.println("Consumer01 Message Content:" + messageContent);
    }

    @RabbitListener(queues = {"sakiko.queue.fanout02"})
    public void processMessage02(String messageContent, Message message, Channel channel) {
        System.out.println("Consumer02 Message Content:" + messageContent);
    }

    // 【路由模式】
    @RabbitListener(queues = {"sakiko.queue.direct01"})
    public void processMessageRouting(String messageContent, Message message, Channel channel) {
        System.out.println("consumer 01 Message Content:" + messageContent);
    }

    @RabbitListener(queues = {"sakiko.queue.direct02"})
    public void processMessageRouting2(String messageContent, Message message, Channel channel) {
        System.out.println("consumer 02 Message Content:" + messageContent);
    }

    // 【主题模式】
    // # ==> 匹配 0个或多个词
    // * ==> 匹配 1个词
    @RabbitListener(queues = {"sakiko.queue.topic01"}) // *.a.*
    public void processMessageTopics01(String messageContent, Message message, Channel channel) {
        System.out.println("consumer 01 Message Content:" + messageContent);
    }

    @RabbitListener(queues = {"sakiko.queue.topic02"}) // *.*.b || c.#
    public void processMessageTopics02(String messageContent, Message message, Channel channel) {
        System.out.println("consumer 02 Message Content:" + messageContent);
    }

}
